package com.heima.wemedia.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.config.MinIOTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.entity.WmMaterial;

import com.heima.model.wmLoginDto.WmMaterialDto;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.dao.WmMaterialDao;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 自媒体素材表(WmMaterial)表服务实现类
 *
 * @author makejava
 * @since 2022-04-10 14:04:26
 */
@Service("wmMaterialService")
public class WmMaterialServiceImpl implements WmMaterialService {
    @Resource
    private WmMaterialDao wmMaterialDao;
    @Autowired
    private MinIOTemplate minIOTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmMaterial queryById(Long id) {
        return this.wmMaterialDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial insert(WmMaterial wmMaterial) {
        this.wmMaterialDao.insert(wmMaterial);
        return wmMaterial;
    }

    /**
     * 修改数据 收藏
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseResult update(Integer id) {
        WmMaterial wmMaterial1 = wmMaterialDao.queryById(id.longValue());
        Integer isCollection = wmMaterial1.getIsCollection();
        if (isCollection == 0) {
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setId(id.longValue());
            wmMaterial.setIsCollection(1);
            this.wmMaterialDao.update(wmMaterial);
            return ResponseResult.okResult(200,"收藏成功");
        }
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId(id.longValue());
        wmMaterial.setIsCollection(0);
        this.wmMaterialDao.update(wmMaterial);
        return ResponseResult.okResult(200,"取消收藏成功");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        this.wmMaterialDao.deleteById(id);
        return ResponseResult.okResult(200,"删除成功");
    }

    /**
     * 自媒体素材上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPic(MultipartFile multipartFile) throws IOException {
        //获取到素材的全名
        String name = multipartFile.getOriginalFilename();
        //判断该素材是否为空
        if (multipartFile == null || multipartFile.getSize() == 0) {
            //返回错误
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //获取上传的文件后缀 与生成的UUID 拼接组成新的文件名
        String fileName = UUID.randomUUID().toString().replaceAll("-", "") + name.substring(name.lastIndexOf("."));
        //将该素材上传至 minio
        String url = minIOTemplate.uploadImgFile("", fileName, multipartFile.getInputStream());
        //将该素材名称获取到 添加到该用户的素材表中
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getId().getUserId());
        wmMaterial.setUrl(url);
        wmMaterial.setType(0);
        wmMaterial.setIsCollection(0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUpdatedTime(new Date());
        wmMaterialDao.insert(wmMaterial);
        //封装素材模型 返回给页面
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 自媒体分页查询
     *
     * @param wmMaterialDto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto wmMaterialDto) {
        if (wmMaterialDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmMaterialDto.checkParam();
        WmMaterial wmMaterial = new WmMaterial();
        //创建分页查询
        Integer page = wmMaterialDto.getPage();
        Integer size = wmMaterialDto.getSize();

        //判断是否 为收藏 如果0收藏 查询收藏数据  //为1查询所有的
        wmMaterial.setUserId(WmThreadLocalUtil.getId().getUserId());
        if (wmMaterialDto.getIsCollection() != null && wmMaterialDto.getIsCollection() == 1) {
            wmMaterial.setIsCollection(wmMaterialDto.getIsCollection());
        }
        PageHelper.startPage(page,size );
        Page<WmMaterial> wmMaterials = wmMaterialDao.queryAllByLimit(wmMaterial);


        PageResponseResult result = new PageResponseResult(page, size, (int)wmMaterials.getTotal());
        result.setData(wmMaterials);
        return result;
    }
}
